package security;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import kernel.bo.EntityObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;

/**
 * 系统用户.
 * 
 * @author fengchi
 */
public class SecUser extends EntityObject implements UserDetails{

    private static final long serialVersionUID = 8585484879324416599L;

    private String username;// 登陆用户名

    private String password;// 密码

    private String partyId;// 关联 party entity
    
    private Date createTime;
    
    private boolean accountNonExpired = true;// 账户是否过期。暂默认值为true，后期根据业务修改

    private boolean accountNonLocked = true;;// 账户是否锁定。暂默认值为true，后期根据业务修改

    private boolean credentialsNonExpired = true;;// 账户密码是否过期。暂默认值为true，后期根据业务修改

    private boolean enabled = true;;// 账户是否有效。暂默认值为true，后期根据业务修改

    private String defaultLocale;// 默认Locale

    private Set<Role> roles = new HashSet<Role>();// 角色
    
    public GrantedAuthority[] getAuthorities() {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>(roles.size());
        for (Role role : roles) {
            grantedAuthorities.add(new GrantedAuthorityImpl("ROLE_" + role.getRoleName()));
        }
        return grantedAuthorities.toArray(new GrantedAuthority[roles.size()]);

    }

    public String getRoleAuthorities() {
        List<String> roleName = new ArrayList<String>();
        for (Role role : roles) {
            roleName.add(role.getRoleName());
        }
        return StringUtils.join(roleName.iterator(), ",");
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }



    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    public String getDefaultLocale() {
        return defaultLocale;
    }

    public void setDefaultLocale(String defaultLocale) {
        this.defaultLocale = defaultLocale;
    }

    public Locale getLocale() {
        if (this.getDefaultLocale() != null) {
            return new Locale(this.getDefaultLocale());
        }
        return null;
    }

    public String getPartyId() {
        return partyId;
    }

    public void setPartyId(String partyId) {
        this.partyId = partyId;
    }

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
    
}
